package com.example.PolimiAssistant;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.Wsdl2Code.WebServices.PolimiAssistant.NomClassroom;
import com.Wsdl2Code.WebServices.PolimiAssistant.PolimiAssistant;
import com.example.PolimiAssistant.common.AlertMessage;
import com.example.PolimiAssistant.common.ClassroomAdapter;
import com.example.PolimiAssistant.common.CommonActivity;
import com.example.PolimiAssistant.utils.PreferencesManager;

import java.util.Vector;


/**
 * Classrooms.java
 *
 * This activity allow the user to search for the building where the
 * searched classroom is.
 *
 * TODO: Map display of the classroom, maybe with dummy data and google maps
 */
public class Classrooms extends CommonActivity {

    private static final String TAG = MainMenu.class.getSimpleName();
    private ListView classroomList;
    private TextView classroomsearch;
    private ClassroomAdapter listAdapter;
    private Vector<NomClassroom> classroomCollection;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.classrooms_screen);

        classroomList = (ListView) findViewById(R.id.classroomList);
        classroomsearch = (TextView) findViewById(R.id.classroomName);

        // Click event for single list row
        classroomList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent i = new Intent();
                i.setClass(Classrooms.this, Classroom_detail.class);
                NomClassroom c_selected = classroomCollection.get(position);
                i.putExtra("floor",c_selected.floor);
                i.putExtra("building",c_selected.building);
                i.putExtra("name",c_selected.name);
                i.putExtra("description",c_selected.description);
                i.putExtra("num_of_seats", String.valueOf(c_selected.num_of_seats));
                i.putExtra("latitude", String.valueOf(c_selected.latitude));
                i.putExtra("longitude",String.valueOf(c_selected.longitude));
                startActivity(i);
            }
        });
    }

    public void Search(View view){
        final Context context = this;
        final String classroom_name = classroomsearch.getText().toString();
        if(classroom_name == null || classroom_name.length() < 2) {
            AlertMessage alert = new AlertMessage(context,"Please enter at least 2 characters");
            alert.Show();
            return;
        }

        new AsyncTask<Void, Void, Vector<NomClassroom>>() {
            ProgressDialog loadingDialog;
            {
                loadingDialog = new ProgressDialog(Classrooms.this);
            }
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog.setMessage("Loading...");
                loadingDialog.show();
            }
            @Override
            protected Vector<NomClassroom> doInBackground(Void... params) {
                PolimiAssistant polimiService = new PolimiAssistant();
                PreferencesManager p = new PreferencesManager(context);
                String token = p.getToken();
                Vector<NomClassroom> classList = polimiService.SearchClassroom(token, classroom_name);

                if(classList != null && classList.size() > 0) {
                    NomClassroom status = classList.get(0);
                    if (!status.valid) {
                        invalidTokenLogOut();
                    }
                }
                if(classList == null) {
                    classList = new Vector<NomClassroom>();
                    Log.e(TAG,"SEARCH FAILURE: SearchClassroom RETURN NULL, classroom_name: " + classroom_name);
                }

                classroomCollection = classList;
                return classList;
            }

            @Override
            protected void onPostExecute(Vector<NomClassroom> list) {
                super.onPostExecute(list);
                listAdapter = new ClassroomAdapter(context,R.layout.classroom_list_item, list);
                classroomList.setAdapter(listAdapter);
                loadingDialog.dismiss();
            }

        }.execute();
    }
}